package com.ljh.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljh.pojo.Collection;
import com.ljh.pojo.Singer;
import com.ljh.pojo.Song;
import com.ljh.pojo.list;
import com.ljh.service.CollectionService;
import com.ljh.service.SongService;
import com.ljh.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("coll")
@Api(description = "专辑接口管理")
@CrossOrigin
public class CollectionController {

    @Autowired
    private CollectionService collectionService;

    @Autowired
    SongService songService;

    /**
     * 随机查询取前九位
     * @return
     */
    @RequestMapping("find")
    @ApiOperation("随机查询取前九位")
    public Result find(){
        return Result.success(collectionService.findCollection());
    }

    @GetMapping("/collectionList")
    @ApiOperation(value = "专辑列表")
    public Result songList(
            @RequestParam Integer current,
            @RequestParam Integer size,
            @RequestParam(required = false) String collName
    ) {
        System.out.println("gogogo");
        IPage<Singer> page = collectionService.page(new Page(current,size),new QueryWrapper<Collection>()
                .like(StrUtil.isNotBlank(collName), "collName",collName));
        return Result.success(page);
    }

    @GetMapping("/deleteCollByCollId")
    @ApiOperation("通过CollId来删除")
    public Result deleteCollById(@RequestParam Integer collId){
        List<Song> songsByCollId = songService.findSongsByCollId(collId);
        for(int i =0;i<songsByCollId.size();i++){
            songService.deleteListSongBySongId(songsByCollId.get(i).getSongId());
        }
        songService.deleteUserByCollid(collId);
        collectionService.deleteCollById(collId);
        return Result.success("修改成功");
    }

    @GetMapping("/updatePhoto")
    @ApiOperation("更新相册")
    public Result updatePhoto(
            @RequestParam Integer collId,
            @RequestParam String collPhoto
    ){
        collectionService.updateListPhoto(collPhoto, collId);
        return Result.success("操作成功");
    }

    @GetMapping("/getCollectionInfo")
    @ApiOperation("获取集合信息")
    public Result getCollectionInfo(
            @RequestParam Integer collId
    ){
        Collection collById = collectionService.findCollById(collId);
        return Result.success(collById);
    }

    @GetMapping("/findListSongs")
    @ApiOperation("通过CollId来找list")
    public Result findListSongs(
            @RequestParam Integer collId
    ){
        List<Song> songsByCollId = songService.findSongsByCollId(collId);
        return Result.success(songsByCollId);
    }

    @GetMapping("/insertColl")
    @ApiOperation("加入Coll")
    public Result insertColl(
            @RequestParam String collName,
            @RequestParam String collDate,
            @RequestParam Integer singerId,
            @RequestParam String singerName
    ){
        collectionService.insertCollection(collName, collDate, singerId, singerName);
        return Result.success("修改成功");
    }

    @GetMapping("/findCollIdNULL")
    @ApiOperation("找没有加入专辑的歌曲")
    public Result findCollIdNULL(){
        List<Song> songByCollIdNull = songService.findSongByCollIdNull();
        return Result.success(songByCollIdNull);
    }

    @GetMapping("/deleteSongById")
    @ApiOperation("在专辑里面删除歌曲")
    public Result deleteSongById(Integer songId){
        songService.deleteSongById(songId);
        return Result.success("操作成功");
    }

    @GetMapping("/insertColl2")
    @ApiOperation("新建集合")
    public Result insertColl2(
            @RequestParam Integer collId,
            @RequestParam String collName,
            @RequestParam Integer songId
    ){
        Collection collById = collectionService.findCollById(collId);
        songService.insertColl(collId, collName, songId,collById.getCollPhoto());
        return Result.success("操作成功");
    }

    @GetMapping("/updateColl")
    @ApiOperation("更新专辑")
    public Result updateColl(
            @RequestParam String collName,
            @RequestParam String singerName,
            @RequestParam String collDate,
            @RequestParam Integer collId
    ){
        collectionService.updateColl(collName, singerName, collDate, collId);
        return Result.success("操作成功");
    }
}
